#include <STC89C5xRC.H>
#include "delay.h"
#include "LCD1602.h"
#include "key.h"
#include "at24c02.h"
#include "i2c.h"

unsigned char keynum;
unsigned int num;

void main()
{
	LCD_Init();
	LCD_ShowString(1,1,"AT24C02");
	LCD_ShowNum(2,1,num,5);

//	at24c02_writebyte(1,200);
//	Delayms(5);
//	Data=at24c02_readbyte(1);
//	LCD_ShowNum(2,1,Data,3);
	while(1)
	{
		keynum = key();
		if(keynum==1)
		{
			num++;
			LCD_ShowNum(2,1,num,5);
		}
		if(keynum==2)
		{
			num--;
			LCD_ShowNum(2,1,num,5);
		}
		if(keynum==3)
		{
			at24c02_writebyte(0,num%256);
			Delayms(5);
			at24c02_writebyte(1,num/256);
			Delayms(5);
			LCD_ShowString(1,9,"write ok");
			Delayms(1000);
			LCD_ShowString(1,9,"        ");
		}
		if(keynum==4)
		{
			num=at24c02_readbyte(0);
			num |=at24c02_readbyte(1)<<8;
			LCD_ShowNum(2,1,num,5);
			LCD_ShowString(1,9,"read ok ");
			Delayms(1000);
			LCD_ShowString(1,9,"        ");
		}
	}

}